<?php $divId = $this->uuid('div', $this->here); ?>
<div id="<?php echo $divId; ?>">
    <?php echo $ajax->form('ItsCustProductSite', 'post', array('update' => $divId, 'indicator' => 'indicator', 'url' => array('action' => 'save'))); ?>
    <?php echo $form->hidden('CustProductSite.id'); ?>
    <?php echo $form->hidden('CustProductSite.site_id'); ?>
    <?php echo $form->hidden('CustProductSite.product_id'); ?>
    <?php echo $form->hidden('mode', array('name' => 'data[mode]', 'value' => $mode)); ?>

    <div class="toolbar">
        <?php if ($mode == 'add') : ?>
        <?php echo $this->element('toolbar', array('buttons' => array('save', array('clear' => array('update' => $divId))))); ?>
        <?php else : // mode edit ?>
        <?php echo $this->element('toolbar', array('buttons' => array('save', array('refresh_view' => array('model' => 'CustProductSite', 'update' => $divId)),
                        array('delete' => array('controller' => 'ItsCustProductSites', 'model' => 'CustProductSite', 'title' => 'stock_code', 'update' => $divId))))); ?>
        <?php endif; ?>
            </div>
            <br />
            <fieldset>
                <legend>Product Information</legend>
                <table>
                    <tr>
                        <td width="100px"><?php echo $form->label('CustProductSite.stock_code', 'Stock Code', array('class' => 'required')) ?></td>
                        <td width="230px">
                    <?php echo $form->text('CustProductSite.stock_code') ?>
                </td>
                <td width="120px"><?php echo $form->label('CustProductSite.dispatch_from', 'Dispatch from', array('class' => 'required')); ?></td>
                <td>
                    <?php if ($mode == 'add' || $this->data['CustProductSite']['current_stock'] == 0) : ?>
                    <?php echo $form->radio('CustProductSite.dispatch_from', array(ITS_PREFIX => 'ITS', SUPPLIER_PREFIX => 'Other Vendor'), array('legend' => false)); ?>
                    <?php else : ?>
                    <?php
                            echo $this->data['CustProductSite']['dispatch_from'] == ITS_PREFIX ?
                                    $this->requestAction('system_preferences/get/company_name') :
                                    ($this->data['CustProductSite']['dispatch_from'] == SUPPLIER_PREFIX ? $this->data['Supplier']['name'] : '-'); ?>
                    <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo $form->label('CustProductSite.price', 'Latest Price') ?></td>
                        <td>
                    <?php echo $form->text('CustProductSite.price') ?> Baht
                    <?php if ($mode == 'edit') : ?>
                                &nbsp;
                    <?php
                                echo $ajax->link($html->image('icon/popup.png', array('title' => 'Price History')),
                                        array('controller' => 'its_cust_product_sites', 'action' => 'popup_view_price', $this->data['CustProductSite']['id']),
                                        array('update' => 'content', 'position' => 'bottom', 'indicator' => 'indicator'), null, false);
                    ?>
                    <?php endif; ?>
                            </td>
                            <td><?php echo $form->label('CustProductSite.stock_out_type', 'Stock Out Type') ?></td>
                            <td>
                    <?php echo $form->radio('CustProductSite.stock_out_type', array('F' => 'FIFO', 'L' => 'Latest Price'), array('legend' => false)); ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="top"><?php echo $form->label('CustProductSite.text', 'Description') ?></td>
                            <td colspan="3">
                    <?php echo $form->textarea('CustProductSite.text', array('cols' => '90', 'rows' => '5')) ?> <br />
                                <span class="caption">(Blank for using ITS product description.)</span>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('CustProductSite.discontinued', 'Discontinued') ?></td>
                            <td>
                    <?php echo $form->checkbox('CustProductSite.discontinued', array('id' => 'CustProductSiteDiscontinued' . $divId)); ?> Yes/No
                            </td>
                        </tr>
                        <tr id="trCPSDiscontinued<?php echo $divId; ?>">
                            <td>&nbsp;</td>
                            <td colspan="3">
                                <div class="caption red">This product are no longer see by customer.</div>
                            </td>
                        </tr>
                    </table>
                </fieldset>
                <fieldset>
                    <legend>Application Information</legend>
                    <table>
                        <tbody>
                            <tr>
                                <td width="80px"><?php echo $form->label('CustProductSite.mc', 'M/C') ?></td>
                                <td width="250px">
                        <?php echo $form->text('CustProductSite.mc') ?>
                            </td>
                            <td width="120px"><?php echo $form->label('CustProductSite.fluid', 'Fluid') ?></td>
                            <td>
                        <?php echo $form->text('CustProductSite.fluid') ?>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('CustProductSite.temp', 'Temp') ?></td>
                            <td>
                        <?php echo $form->text('CustProductSite.temp') ?>
                            </td>
                            <td><?php echo $form->label('CustProductSite.pressure', 'Pressure') ?></td>
                            <td>
                        <?php echo $form->text('CustProductSite.pressure') ?> psi.
                            </td>
                        </tr>
                    </tbody>
                </table>
            </fieldset>
            <fieldset>
                <legend>Moving Information</legend>
                <table>
                    <tbody>
                        <tr>
                            <td width="180px"><?php echo $form->label('CustProductSite.ex_stock', 'Ex-stock before Consigned') ?></td>
                            <td width="70px"><?php echo $form->text('CustProductSite.ex_stock', array('size' => 5, 'maxlength' => 5)) ?> <br /></td>
                            <td width="70px"><span class="unit"><?php
                                if (isset($this->data['Product']['Unit']['title'])) {
                                    echo $this->data['Product']['Unit']['title'];
                                }
                        ?></span></td>
                        <td width="175px"><?php echo $form->label('CustProductSite.critical_part', 'Critical part') ?></td>
                        <td><?php echo $form->checkbox('CustProductSite.critical_part') ?> Yes/No</td>
                    </tr>
                    <tr>
                        <td><?php echo $form->label('CustProductSite.min_stock', 'Min Stock') ?></td>
                        <td><?php echo $form->text('CustProductSite.min_stock', array('size' => 5, 'maxlength' => 5)) ?> <br /></td>
                        <td><span class="unit"><?php
                                if (isset($this->data['product']['Unit']['title'])) {
                                    echo $this->data['Product']['Unit']['title'];
                                }
                        ?></span></td>
                        <td><?php echo $form->label('CustProductSite.critical_mc', 'Critical M/C') ?></td>
                        <td><?php echo $form->checkbox('CustProductSite.critical_mc', array('size' => 5, 'maxlength' => 5)) ?> Yes/No </td>
                    </tr>
                    <tr>
                        <td><?php echo $form->label('CustProductSite.max_stock', 'Max stock') ?></td>
                        <td><?php echo $form->text('CustProductSite.max_stock', array('size' => 5, 'maxlength' => 5)) ?> <br /></td>
                        <td><span class="unit"><?php
                                if (isset($this->data['Product']['Unit']['title'])) {
                                    echo $this->data['Product']['Unit']['title'];
                                }
                        ?></span></td>
                        <td><?php echo $form->label('CustProductSite.cycle_time', 'Cycle time') ?></td>
                        <td>
                        <?php echo $form->text('CustProductSite.cycle_time', array('size' => 5, 'maxlength' => 5)) ?>
                        <?php echo $form->select('CustProductSite.cycle_time_period_id', $periods) ?>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('CustProductSite.current_stock', 'Current Stock') ?></td>
                            <td class="text-right"><?php echo ($mode == 'edit') ? $this->data['CustProductSite']['current_stock'] : '&nbsp;'; ?></td>
                            <td><span class="unit"><?php
                                if (isset($this->data['Product']['Unit']['title'])) {
                                    echo $this->data['Product']['Unit']['title'];
                                }
                        ?></span></td>
                        <td><?php echo $form->label('CustProductSite.shortest_life_cycle', 'Shortest Life Cycle') ?></td>
                        <td>
                        <?php echo $form->text('CustProductSite.shortest_life_cycle', array('size' => 5, 'maxlength' => 5)) ?>
                        <?php echo $form->select('CustProductSite.shortest_life_cycle_period_id', $periods) ?>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('CusProductSite.last_stock_in_date', 'Last Stock in Date'); ?></td>
                            <td class="text-right"><?php echo!empty($this->data['CustProductSite']['last_stock_in_date']) ? $this->data['CustProductSite']['last_stock_in_date'] : '-' ?></td>
                            <td>&nbsp;</td>
                            <td><?php echo $form->label('CustProductSite.maintenance_plan', 'Maintenance plan(every)') ?></td>
                            <td>
                        <?php echo $form->text('CustProductSite.maintenance_plan', array('size' => 5, 'maxlength' => 5)) ?>
                        <?php echo $form->select('CustProductSite.maintenance_plan_period_id', $periods) ?>
                            </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('CusProductSite.last_stock_out_date', 'Last Stock out Date'); ?></td>
                            <td class="text-right"><?php echo!empty($this->data['CustProductSite']['last_stock_out_date']) ? $this->data['CustProductSite']['last_stock_out_date'] : '-' ?></td>
                            <td>&nbsp;</td>
                            <td><?php echo $form->label('CustProductSite.complete_consignment_date', 'Complete consignment on') ?></td>
                            <td>&nbsp;</td>
                        </tr>
                    </tbody>
                </table>
            </fieldset>
    <?php echo $form->end(); ?>

                                <fieldset>
                                    <legend>Available Locations</legend>
        <?php if ($mode == 'edit') : ?>
                                    <div id="divCPSLocations">
            <?php echo $this->requestAction('its_cust_product_sites/display_locations/' . $this->data['CustProductSite']['id'], array('return')); ?>
                                </div>
        <?php else : ?>
                                        <div class="caption">Please save customer product information first.</div>
        <?php endif; ?>
                                    </fieldset>
                                </div>

                                <script type="text/javascript">
                                    $j(function() {
                                        $j('#CustProductSiteDiscontinued<?php echo $divId; ?>').bind('change', function() {
                                            if ($j(this).is(':checked') == true) {
                                                $j('#trCPSDiscontinued<?php echo $divId; ?>').show();
                                            } else {
                                                $j('#trCPSDiscontinued<?php echo $divId; ?>').hide();
            }
        }).change();
    });
</script>